/**
 * Copyright (c) 2008 - TheNittyGritty - Thomas Brekelmans
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package nl.thenittygritty.amc 
{
	import flash.utils.Dictionary;	
	
	/**
	 * ModuleRegistry contains references to all the modules inside an 
	 * application. Each module can use the registry to access another module
	 * and directly invoke a method on the module.
	 * ModuleRegistry is a singleton.
	 */
	public class ModuleRegistry 
	{
		private static var instance:ModuleRegistry;
		
		private var moduleStore:Dictionary;
		
		/**
		 * Creates a new ModuleRegistry.
		 * Since this class is a singleton, an enforcer is used to prevent
		 * instatiation of the class from the outside. The static getInstance
		 * method is used to ensure only one instance exists at any given time.
		 */
		public function ModuleRegistry(singletonEnforcer:SingletonEnforcer) 
		{
			moduleStore = new Dictionary();
		}
		
		/**
		 * Returns the sole instance of ModuleRegistry. 
		 * If no instance is created yet, it is created here first before it
		 * is returned.
		 */
		public static function getInstance():ModuleRegistry
		{
			if (ModuleRegistry.instance == null)
			{
				ModuleRegistry.instance = new ModuleRegistry(
													   new SingletonEnforcer());	
			}	
			return ModuleRegistry.instance;
		}
		
		/**
		 * Stores the given module under the given name. 
		 * The name used here should be used to retrieve and/or delete the given 
		 * module from the registry as well.
		 * Preferably store your module names in your project's 
		 * ApplicationSettings class.
		 */
		public function storeModule(moduleName:String, module:Module):void
		{
			moduleStore[moduleName] = module;
		}
		
		/**
		 * Returns the module instance stored under the given name.
		 * If nothing is stored under the given name, undefined is returned.
		 */
		public function retrieveModule(moduleName:String):Module
		{
			return moduleStore[moduleName];
		}
		
		/**
		 * Removes the module stored under the given name from the registry.
		 * If noting is stored under the given name, nothing happens and the 
		 * registry is unaffected.
		 */
		public function deleteModule(moduleName:String):void
		{
			delete moduleStore[moduleName];
		}
		
		/**
		 * Returns the name of the class in a String.
		 */
		public function toString():String 
		{
			return "ModuleRegistry";
		}
	}
}
/**
 * SingletonEnforcer, special class to be able to enforce the use of the above 
 * Singleton class based on the Singleton pattern.
 * This must be placed outside the package to ensure it can only be called from 
 * within this class file.
 */
internal class SingletonEnforcer {}